<template>
    <el-dialog
            :title="!dataForm.orderId ? '新增' : '修改'"
            :close-on-click-modal="false"
            :visible.sync="visible">
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
                            <el-form-item label="主键ID" prop="id">
                        <el-input v-model.number="dataForm.id" placeholder="主键ID"></el-input>
                </el-form-item>
                <el-form-item label="事件类型（如设备故障、能源供应中断、火灾等）" prop="eventType">
                        <el-input v-model="dataForm.eventType" placeholder="事件类型（如设备故障、能源供应中断、火灾等）"></el-input>
                </el-form-item>
                <el-form-item label="事件发生时间" prop="eventTime">
                        <el-date-picker
                                v-model="dataForm.eventTime"
                                type="datetime"
                                placeholder="事件发生时间">
                        </el-date-picker>
                </el-form-item>
                <el-form-item label="事件发生地点" prop="eventLocation">
                        <el-input v-model="dataForm.eventLocation" placeholder="事件发生地点"></el-input>
                </el-form-item>
                <el-form-item label="事件状态（如未处理、处理中、已处理）" prop="eventStatus">
                        <el-input v-model="dataForm.eventStatus" placeholder="事件状态（如未处理、处理中、已处理）"></el-input>
                </el-form-item>
                <el-form-item label="责任人" prop="responsiblePerson">
                        <el-input v-model="dataForm.responsiblePerson" placeholder="责任人"></el-input>
                </el-form-item>
                <el-form-item label="创建时间" prop="createTime">
                        <el-date-picker
                                v-model="dataForm.createTime"
                                type="datetime"
                                placeholder="创建时间">
                        </el-date-picker>
                </el-form-item>
                <el-form-item label="更新时间" prop="updateTime">
                        <el-date-picker
                                v-model="dataForm.updateTime"
                                type="datetime"
                                placeholder="更新时间">
                        </el-date-picker>
                </el-form-item>
                <el-form-item label="事件原因" prop="eventCause">
                        <el-input v-model="dataForm.eventCause" placeholder="事件原因"></el-input>
                </el-form-item>
                <el-form-item label="事件影响" prop="eventImpact">
                        <el-input v-model="dataForm.eventImpact" placeholder="事件影响"></el-input>
                </el-form-item>
                <el-form-item label="处理措施" prop="handlingMeasures">
                        <el-input v-model="dataForm.handlingMeasures" placeholder="处理措施"></el-input>
                </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
    </el-dialog>
</template>

<script>
    export default {
        data () {
            return {
                visible: false,
                dataForm: {
                    id: null,
                    eventType: null,
                    eventTime: null,
                    eventLocation: null,
                    eventStatus: null,
                    responsiblePerson: null,
                    createTime: null,
                    updateTime: null,
                    eventCause: null,
                    eventImpact: null,
                    handlingMeasures: null,
                },
                dataRule: {
                    id: [
                        { required: true, message: '主键ID不能为空', trigger: 'blur' }
                    ],
                    eventType: [
                        { required: true, message: '事件类型（如设备故障、能源供应中断、火灾等）不能为空', trigger: 'blur' }
                    ],
                    eventTime: [
                        { required: true, message: '事件发生时间不能为空', trigger: 'blur' }
                    ],
                    eventLocation: [
                        { required: true, message: '事件发生地点不能为空', trigger: 'blur' }
                    ],
                    eventStatus: [
                        { required: true, message: '事件状态（如未处理、处理中、已处理）不能为空', trigger: 'blur' }
                    ],
                    responsiblePerson: [
                        { required: true, message: '责任人不能为空', trigger: 'blur' }
                    ],
                    createTime: [
                        { required: true, message: '创建时间不能为空', trigger: 'blur' }
                    ],
                    updateTime: [
                        { required: true, message: '更新时间不能为空', trigger: 'blur' }
                    ],
                    eventCause: [
                        { required: true, message: '事件原因不能为空', trigger: 'blur' }
                    ],
                    eventImpact: [
                        { required: true, message: '事件影响不能为空', trigger: 'blur' }
                    ],
                    handlingMeasures: [
                        { required: true, message: '处理措施不能为空', trigger: 'blur' }
                    ],
                }
            }
        },
        methods: {
            init (id) {
                this.dataForm.orderId = id || 0
                this.visible = true
                this.$nextTick(() => {
                    this.$refs['dataForm'].resetFields()
                    this.dataForm.orderId = id || 0
                    if (this.dataForm.orderId) {
                        this.$http({
                            url: this.$http.adornUrl(`/app/AppEnergySafetyEventManagement/info/\\$\\{this.dataForm.orderId}`),
                            method: 'get',
                            params: this.$http.adornParams()
                        }).then(({data}) => {
                            if (data && data.code === 0) {
                                this.dataForm = data.appenergysafetyeventmanagement
                            }
                        })
                    }
                })
            },
            // 表单提交
            dataFormSubmit () {
                this.$refs['dataForm'].validate((valid) => {
                    if (valid) {
                        this.$http({
                            url: this.$http.adornUrl(`/app/AppEnergySafetyEventManagement/\\$\\{!this.dataForm.orderId ? 'save' : 'update'}`),
                            method: 'post',
                            data: this.$http.adornData(this.dataForm)
                        }).then(({data}) => {
                            if (data && data.code === 0) {
                                this.$message({
                                    message: '操作成功',
                                    type: 'success',
                                    duration: 1500,
                                    onClose: () => {
                                        this.visible = false
                                        this.$emit('refreshDataList')
                                    }
                                })
                            } else {
                                this.$message.error(data.msg)
                            }
                        })
                    }
                })
            }
        }
    }
</script>